package com.equipmentRent.web.controller;



import com.equipmentRent.common.core.domain.AjaxResult;
import com.equipmentRent.common.core.domain.entity.SysUser;
import com.equipmentRent.common.utils.DateUtils;
import com.equipmentRent.common.utils.ShiroUtils;
import com.equipmentRent.framework.shiro.service.SysPasswordService;
import com.equipmentRent.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static com.equipmentRent.common.core.domain.AjaxResult.error;
import static com.equipmentRent.common.core.domain.AjaxResult.success;
import static com.equipmentRent.common.utils.ShiroUtils.getSysUser;


@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;


    @GetMapping("/loginPage")
    public String loginPage() {
        return "user/login";
    }


    @GetMapping("/index")
    public String index() {
        return "user/index";
    }


    @GetMapping("/logOut")
    public String logOut() {
        ShiroUtils.logout();
        return "user/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult userLogin(String username, String password, Boolean rememberMe)
    {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
            return success();
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    /**
     * 获取用户信息
     * @return
     */
    @PostMapping("/getUserInfo")
    @ResponseBody
    public AjaxResult getUserInfo(){
        SysUser sysUser = getSysUser();
        return success(sysUser);
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(String oldPassword, String newPassword)
    {
        SysUser user = getSysUser();
        if (!passwordService.matches(user, oldPassword))
        {
            return error("修改密码失败，旧密码错误");
        }
        if (passwordService.matches(user, newPassword))
        {
            return error("新密码不能与旧密码相同");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
        user.setPwdUpdateDate(DateUtils.getNowDate());
        if (userService.resetUserPwd(user) > 0)
        {
            ShiroUtils.setSysUser(userService.selectUserById(user.getUserId()));
            return success();
        }
        return error("修改密码异常，请联系管理员");
    }

}
